package coop.bancocredicoop.proyectos.gd.service;

import org.hibernate.Query;
import org.hibernate.classic.Session;

import coop.bancocredicoop.proyectos.gd.domain.Box;
import coop.bancocredicoop.proyectos.gd.query.DataService;

public class BoxService extends DataService {

	public Box getBoxByCajaId(Session session, String cajaId) {		
		Query query = session.createQuery("from Box where cajaId = :cajaId");
		query.setString("cajaId", cajaId);
		query.setMaxResults(1);
		return (Box) query.uniqueResult();			
	}
	
	public Box createBox(Session session, String cajaId) {
		Box newBox = new Box(cajaId);
		session.save(newBox);
		return newBox;
	}
	
    public Box getOrCreateBox(Session session, String cajaId) throws Exception {
        Box box = getBoxByCajaId(session, cajaId);
        if (box == null) {
        	return createBox(session, cajaId);
        } else {
            return box;
        }    
    }
	
}
